﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using Wedn.Core.Utility;

namespace Wedn.Web.UI.Admin.Handler
{
    /// <summary>
    /// image_manager 的摘要说明
    /// </summary>
    public class ImageManager : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string path = "upload"; //需要遍历的目录列表，最好使用缩略图地址，否则当网速慢时可能会造成严重的延时
            string[] filetype = { ".gif", ".png", ".jpg", ".jpeg", ".bmp" };                //文件允许格式

            string action = context.Server.HtmlEncode(context.Request["action"]);

            if (action == "get")
            {
                //String str = String.Empty;
                //DirectoryInfo info = new DirectoryInfo(context.Server.MapPath(Config.SiteRoot + path));

                ////目录验证
                //if (info.Exists)
                //{
                //    
                //    DirectoryInfo[] infoArr = info.GetDirectories();
                //    foreach (DirectoryInfo tmpInfo in infoArr)
                //    {
                //        foreach (DirectoryInfo tmpInfo2 in tmpInfo.GetDirectories())
                //        {
                //            foreach (FileInfo fi in tmpInfo2.GetFiles())
                //            {
                //                if (Array.IndexOf(filetype, fi.Extension) != -1)
                //                {
                //                    str += path + "/" + tmpInfo.Name + "/" + tmpInfo2.Name + "/" + fi.Name + "ue_separate_ue";
                //                }
                //            }
                //        }
                //    }
                //}

                DirectoryInfo dirInfo = new DirectoryInfo(context.Server.MapPath(Config.SiteRoot + path));
                FileInfo[] files = dirInfo.GetFiles("*.*", SearchOption.AllDirectories);
                //string[] files = Directory.GetFiles(context.Server.MapPath(Config.SiteRoot + path), "*.*", SearchOption.AllDirectories);
                if (files.Length > 0)
                {
                    //存在文件情况
                    StringBuilder sbd = new StringBuilder();
                    foreach (var item in files)
                    {
                        string ext = item.Extension;
                        if (filetype.Contains<string>(ext))
                        {
                            //子目录安装可能有问题
                            string tmp = item.FullName.Replace(context.Request.PhysicalApplicationPath, "").Replace("\\", "/");
                            sbd.Append(tmp + "ue_separate_ue");
                        }
                    }
                    context.Response.Write(sbd.ToString());
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}